/**
 * Created by jackliu on 16/11/29.
 */

var express = require('express');
var router = express.Router();
var qiniu = require('qiniu');
var superagent = require('superagent');
var async = require( 'async'); // 同步控制
var moment = require('moment');  // 日期格式化


var ItbTemplateModel = require('../../../common/mysql/mapping/ItbTemplateModel');
var ItbTmplPageModel = require('../../../common/mysql/mapping/ItbTmplPageModel');
var ItbDocumentModel = require('../../../common/mysql/mapping/ItbDocumentModel');
var ItbPageModel = require('../../../common/mysql/mapping/ItbPageModel');
var ItbUserModel = require('../../../common/mysql/mapping/ItbUserModel');

var CONFIG = require('../../../common/config');
var CODE = require('../../../common/code');
var BASE_IP = CONFIG.BASE_IP;

var ParserClassLoader = require('./../../sjg/kernel/base/ParserClassLoader');
ParserClassLoader.loadKernelClass();// 加载类加载器

var homeUtil = (function () {
    function homeUtil() {
    }

    //------------------------------------------------------------------
    // 返回json
    homeUtil.ret_func_json = function (success, msg, data, res){
        var obj = {
            success: success,
            msg: msg,
            data: data
        };
        // 返回
        res.set('Content-Type', 'application/json');
        res.json(obj);
    }
    //------------------------------------------------------------------
    // 返回json
    homeUtil.getDocByDocId = function (tmpl_flg, doc_id, callback){
        var entity;
        if (tmpl_flg == 1) {
            // 模版
            entity = new ItbTemplateModel({
                tmpl_id:doc_id,
                del_flg:0
            });

        } else {
            // 用户文档
            entity = new ItbDocumentModel({
                doc_id:doc_id,
                del_flg:0
            });
        }
        entity.findFromDB(function (error, items) {
            if (error) {
                return callback(error);
            }

            // 未检索到文档返回[]
            if (items.length == 0) {
                return callback(null,null);
            } else {
                return callback(null, items[0]);
            }
        });
    }
    //------------------------------------------------------------------
    // 返回json
    homeUtil.getPagesByDoc = function (tmpl_flg, doc_item,backup_flg, callback){
        var entity2;
        if (tmpl_flg == 1) {
            // 模版
            entity2 = new ItbTmplPageModel({
                tmpl_ids: doc_item.tmpl_id || doc_id
            });

        } else {
            // 用户文档
            entity2 = new ItbTmplPageModel({
                tmpl_ids: doc_item.base_doc_id || '-1'
            });
        }

        entity2.getPagesInfoByTmplIds(function(error, items){
            if(error){
                return callback(error,null);
            }

            // process success
            var pageData = [];
            if (items != null && items.length > 0) {
                for (var i in items){
                    var item = items[i];
                    if (backup_flg == 0) {// 只获取普通页
                        if (Number(item.backup_flg)>0) continue;// 过滤保留页
                    }

                    // 只要有未生成预览图的页面就返回error
                    if (item.thumb_s == null || item.thumb_s.trim().length == 0) {
                        return callback("没有图片",null);
                    }

                    // 组合page数据
                    var pageItem = {
                        base_doc_id: item.tmpl_id,
                        base_page_id: item.page_id,
                        page_index: item.page_index,
                        image: item.thumb_s
                    };
                    pageData.push(pageItem);
                }
            }

            return callback(null,pageData);
        });
    }
    //------------------------------------------------------------------
    // 返回json
    homeUtil.getUserInfo = function (user_id, callback){
        var entity = new ItbUserModel({
                user_id:user_id,
                del_flg:0
            });
        entity.findFromDB(function (error, items) {
            if (error) {
                return callback(error);
            }

            // 未检索到文档返回[]
            if (items.length == 0) {
                return callback(null,null);
            } else {
                return callback(null, items[0]);
            }
        });
    }


    //------------------------------------------------------------------

    return homeUtil;
})();
//------------------------------------------------------------------
module.exports = homeUtil;